package mi.payment.utils;

import android.os.RemoteException;
import android.text.TextUtils;

import com.sunmi.pay.hardware.aidl.AidlConstants;
import com.sunmi.pay.hardware.aidl.system.BasicOpt;

import java.util.HashMap;

import mi.payment.MyApplication;
import sunmi.sunmiui.utils.LogUtil;

/**
 * Created by shz on 2017/9/12.
 * 按照新的sn处理方式获取到sn/tusn号
 */

public class SNUtils {
    private static HashMap<Character, String> initMaps() {
        HashMap<Character, String> maps = new HashMap<Character, String>();
        maps.put('1', "01");
        maps.put('2', "02");
        maps.put('3', "03");
        maps.put('4', "04");
        maps.put('5', "05");
        maps.put('6', "06");
        maps.put('7', "07");
        maps.put('8', "08");
        maps.put('9', "09");
        maps.put('A', "10");
        maps.put('B', "11");
        maps.put('C', "12");
        maps.put('D', "13");
        maps.put('E', "14");
        maps.put('F', "15");
        maps.put('G', "16");
        maps.put('H', "17");
        maps.put('J', "18");
        maps.put('K', "19");
        maps.put('L', "20");
        maps.put('M', "21");
        maps.put('N', "22");
        maps.put('P', "23");
        maps.put('Q', "24");
        maps.put('R', "25");
        maps.put('S', "26");
        maps.put('T', "27");
        maps.put('U', "28");
        maps.put('V', "39");
        maps.put('W', "30");
        maps.put('X', "31");
        maps.put('a', "10");
        maps.put('b', "11");
        maps.put('c', "12");
        maps.put('d', "13");
        maps.put('e', "14");
        maps.put('f', "15");
        maps.put('g', "16");
        maps.put('h', "17");
        maps.put('j', "18");
        maps.put('k', "19");
        maps.put('l', "20");
        maps.put('m', "21");
        maps.put('n', "22");
        maps.put('p', "23");
        maps.put('q', "24");
        maps.put('r', "25");
        maps.put('s', "26");
        maps.put('t', "27");
        maps.put('u', "28");
        maps.put('v', "29");
        maps.put('w', "30");
        maps.put('x', "31");
        return maps;
    }

    // 非21号文上送
    public static String getBindSN() {
        String tusn = "";
        String sn = "";
        BasicOpt basicOpt = MyApplication.getInstance().basicOptBinder;
        if (basicOpt == null) {
            return "";
        }
        try {
            sn = basicOpt.getSysParam(AidlConstants.SysParam.SN);
            tusn = basicOpt.getSysParam(AidlConstants.SysParam.TUSN);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
        LogUtil.e("shz", "get from basicOpt sn = " + sn + ", tusn = " + tusn);
        if (TextUtils.isEmpty(tusn) && TextUtils.isEmpty(sn)) {
            return "";
        } else if (TextUtils.isEmpty(tusn)) {
            return sn;
        } else if (!TextUtils.isEmpty(sn) && tusn.endsWith(sn)) {
            HashMap<Character, String> maps = initMaps();
            if (tusn.length() < 16) {
                return "";
            }
            String year = tusn.substring(12, 14);
            String month = getNumber(maps, true, (tusn.charAt(14)));
            String day = getNumber(maps, false, (tusn.charAt(15)));
            if ("00".equals(month) || "00".equals(day)) {
                return sn;
            }
            try {
                String date = year + month + day;
                int dataInteger = Integer.parseInt(date);
                if (dataInteger > 170730) {
                    return tusn;
                } else {
                    return sn;
                }
            } catch (Exception e) {
                LogUtil.e("shz", " now sn = " + sn + ", tusn = " + tusn);
                return sn;
            }
        } else {
            return tusn;
        }
    }

    private static String getNumber(HashMap<Character, String> maps, boolean isMonth, char ch) {
        if (!maps.containsKey(ch)) {
            return "00";
        }
        if (isMonth && (121 > ch && ch > 99 || 89 > ch && ch > 67)) {
            return "00";
        }
        String value = maps.get(ch);
        return value;
    }

    // 上送21号文时使用此方法来获取TUSN
    public static String getTUSN() {
        String tusn = "";
        String sn = "";
        BasicOpt basicOpt = MyApplication.getInstance().basicOptBinder;
        try {
            sn = basicOpt.getSysParam(AidlConstants.SysParam.SN);
            tusn = basicOpt.getSysParam(AidlConstants.SysParam.TUSN);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
        if (TextUtils.isEmpty(tusn) && TextUtils.isEmpty(sn)) {
            return "";
        } else if (TextUtils.isEmpty(tusn)) {
            return "00004304" + sn;
        } else {
            return tusn;
        }
    }

    /**
     * 判断机器是否有写入TUSN
     *
     * @return
     */
    public static boolean hasTUSN() {
        boolean hasTusn = false;
        String tusn = "";
        String sn = "";
        BasicOpt basicOpt = MyApplication.getInstance().basicOptBinder;
        try {
            sn = basicOpt.getSysParam(AidlConstants.SysParam.SN);
            tusn = basicOpt.getSysParam(AidlConstants.SysParam.TUSN);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
        LogUtil.e("shz", "get from basicOpt sn = " + sn + ", tusn = " + tusn);
        if (TextUtils.isEmpty(tusn) && TextUtils.isEmpty(sn)) {
            hasTusn = false;
        } else if (TextUtils.isEmpty(tusn)) {
            hasTusn = false;
        } else if (!TextUtils.isEmpty(sn) && tusn.endsWith(sn)) {
            if (tusn.length() < 16) {
                return false;
            }
            HashMap<Character, String> maps = initMaps();
            String year = tusn.substring(12, 14);
            String month = getNumber(maps, true, (tusn.charAt(14)));
            String day = getNumber(maps, false, (tusn.charAt(15)));
            if ("00".equals(month) || "00".equals(day)) {
                return false;
            }
            try {
                String date = year + month + day;
                int dataInteger = Integer.parseInt(date);
                if (dataInteger > 170730) {
                    hasTusn = true;
                } else {
                    hasTusn = false;
                }
            } catch (Exception e) {
                LogUtil.e("shz", " now sn = " + sn + ", tusn = " + tusn);
                hasTusn = false;
            }
        } else {
            hasTusn = true;
        }
        return hasTusn;
    }
}